/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.io.Serializable;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.swing.transaction.panel.SearchPanel;
import org.balisunrise.common.EntityFilter;
import org.balisunrise.common.EntityGrid;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.abstracts.AbstractVisualEntity;

/**
 *
 * @author Glauco
 */
public class Search extends AbstractVisualEntity
        implements org.balisunrise.common.Search
        <EntityFilter<JPanel>,
         EntityGrid<JPanel>,
         EntityActions<?,JPanel>, JPanel>{

    private SearchPanel visualRepresentation;
    private EntityFilter entityFilter;
    private EntityGrid entityGrid;
    private EntityActions entityActions;
    private EntityLoader entityLoader;

    public EntityLoader getEntityLoader() {
        return entityLoader;
    }

    public void setEntityLoader(EntityLoader entityLoader) {

        this.entityLoader = entityLoader;

        if(entityGrid != null)
            entityGrid.setEntityLoader(entityLoader);

        if(entityFilter != null)
            entityFilter.setEntityLoader(entityLoader);
    }

    public void setVisualRepresentation(SearchPanel visualRepresentation) {
        this.visualRepresentation = visualRepresentation;
    }

    public void setEntityFilter(EntityFilter filter) {
        
        this.entityFilter = filter;

        if(entityLoader != null)
            entityFilter.setEntityLoader(entityLoader);
    }

    public EntityFilter<JPanel> getEntityFilter() {
        return entityFilter;
    }

    public void setEntityGrid(EntityGrid grid) {
        
        this.entityGrid = grid;

        if(entityLoader != null)
            grid.setEntityLoader(entityLoader);
    }

    public EntityGrid<JPanel> getEntityGrid() {
        return entityGrid;
    }

    public void setEntityActions(EntityActions actions) {
        this.entityActions = actions;
    }

    public EntityActions<?,JPanel> getEntityActions() {
        return entityActions;
    }

    public void newSearch() {

        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
                getEntityGrid().getEntityLoader().load(null);
                EntityFilter<JPanel> ef = getEntityFilter();
                if (ef != null){
                    for (org.balisunrise.common.FieldLayout fl : ef.getFieldLayouts()) {
                        if(fl.requestFocus())
                            break;
                    }
                }
            }
        });
    }

    public Serializable getSelectedEntity() {

        EntityGrid eg = getEntityGrid();

        if (eg != null)
            return eg.getSelectedEntity();
        
        return null;

    }

    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará SEARCH.
     * @return VisualEntityType.SEARCH
     */
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.SEARCH;
    }
}
